﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Viewer.BusinessObject.BusinessEntities;
using Ext.Net;

namespace Viewer.NhanSu.UserControl
{
    public partial class UCPhuCapKhac : System.Web.UI.UserControl
    {
        NSPhuCapKhac phuCapKhac = new NSPhuCapKhac();
        NSNhanVien nhanvien = new NSNhanVien();
        CommonCode common = new CommonCode();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        private void ResetControl()
        {
            txtPhuCapKhac.Text = string.Empty;
            dtNgayHetHieuLuc.Text = DateTime.Today.ToString("dd/MM/yyyy");
            dtNgayHieuLuc.Text = DateTime.Today.ToString("dd/MM/yyyy");
            txtGhiChu.Text = string.Empty;
            txtHeSo.Text = string.Empty;
        }
        [DirectMethod]
        public void ShowDialog(string type, string hdId, string nhanvienID, string typeParent)
        {
        
            hdNhanVienId.Text = nhanvienID;
            hdPhucCapId.Text = hdId;
            if (type.Equals("AddNew"))
            {
                wdPhuCapKhac.Title = "Tạo mới phụ cấp";
                ResetControl();
                hdType.Text = type;
                hdParentType.Text = typeParent;
                btnSave.Hidden = false;
            }
            else if (type.Equals("Edit"))
            {
                wdPhuCapKhac.Title = "Sửa đổi phụ cấp";
                hdType.Text = type;
                btnSave.Hidden = false;
                LoadDataPCK(Convert.ToInt16(hdId));
            }
            else if (type.Equals("View"))
            {
                wdPhuCapKhac.Title = "Xem chi tiết phụ cấp";
                hdType.Text = type;
                btnSave.Hidden = true;
                LoadDataPCK(Convert.ToInt16(hdId));
            }
            wdPhuCapKhac.Show();
        }
        public void LoadDataPCK(int hdId)
        {
            List<NSPhuCapKhac> lst;
            lst = (List<NSPhuCapKhac>)Session["PCK"];
            NSPhuCapKhac item = lst.Where(a => a.PhuCapKhacId.Equals(hdId)).SingleOrDefault();
            dtNgayHieuLuc.Text = item.NgayHieuLuc.ToString("dd/MM/yyyy");
            dtNgayHetHieuLuc.Text = item.NgayHetHieuLuc.HasValue ? item.NgayHetHieuLuc.Value.ToString("dd/MM/yyyy") : string.Empty;
            txtGhiChu.Text = item.GhiChu;
            txtHeSo.Text = item.HeSoPhuCap.ToString();
            txtPhuCapKhac.Text = item.LoaiPhuCapKhac;

        }
        protected void btnSavePCCV_CLick(object sender, DirectEventArgs e)
        {
            DateTime ngayHieuLuc = Convert.ToDateTime(dtNgayHieuLuc.Text);
            DateTime ngayHetHieuLuc = Convert.ToDateTime(dtNgayHetHieuLuc.Text);
            int nhanVienId = !string.IsNullOrEmpty(hdNhanVienId.Text) ? Convert.ToInt16(hdNhanVienId.Text) : 0;
          
            decimal heSo = !string.IsNullOrEmpty(txtHeSo.Text) ? Convert.ToDecimal(txtHeSo.Text) : 0;
            if (hdType.Text.Equals("AddNew"))
            {
                //Trường hợp form parent là addnew
                NSPhuCapKhac item = phuCapKhac.GetPhuCapKhac(nhanVienId, heSo, txtPhuCapKhac.Text, ngayHieuLuc, ngayHetHieuLuc, txtGhiChu.Text);
                ((UCNhanVien)this.Parent).GetPhuCapKhac(item);
            }
            else if (hdType.Text.Equals("Edit"))
            {
                phuCapKhac.EditPCK(Convert.ToInt16(hdPhucCapId.Text), heSo, txtPhuCapKhac.Text, ngayHieuLuc, ngayHetHieuLuc, txtGhiChu.Text);
                ((UCNhanVien)this.Parent).ReloadGrid("PCK");
            }
            X.Mask.Hide();
            wdPhuCapKhac.Hide();

        }
    }
}